// WizzardPage

#include "stdafx.h"
#include "SetupBootstrapLib.h"
#include "SetupBootstrapLibInternal.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


CWizardPage::CWizardPage(CString background, LPCTSTR lpszTemplateName, CWnd* pParent)
                   :CDialog(lpszTemplateName,pParent)
{
	backgroundFile = background;
	m_bCreated = FALSE;
	m_bActive = FALSE;
	m_nDialogID = 0;
	m_pParent = NULL; 
}


CWizardPage::CWizardPage(CString background, UINT nIDTemplate, CWnd* pParent)
                   :CDialog(nIDTemplate,pParent)
{
	backgroundFile = background;
  m_bCreated = FALSE;
  m_bActive = FALSE;
  m_nDialogID = nIDTemplate;
  m_pParent = NULL; 
}

CWizardPage::~CWizardPage()
{
}



void CWizardPage::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWizardPage)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CWizardPage, CDialog)
	//{{AFX_MSG_MAP(CWizardPage)
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP


END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWizardPage message handlers


BOOL CWizardPage::OnInitDialog() 
{
	CDialog::OnInitDialog();

#ifdef DEBUG
	  DWORD style = GetStyle();
	  ASSERT((style & WS_CHILD) != 0);
	  ASSERT((style & WS_BORDER) == 0);
	  ASSERT((style & WS_DISABLED) != 0);
#endif

  return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


BOOL CWizardPage::OnCreatePage()
{
	return TRUE;
}

void CWizardPage::OnDestroyPage()
{
}


HBRUSH CWizardPage::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	switch (nCtlColor)
	{	   
		case CTLCOLOR_STATIC:
			pDC->SetTextColor(RGB(0, 0, 0));
			pDC->SetBkColor(RGB(255,255,255));
			return 0;

		case CTLCOLOR_EDIT:
			pDC->SetTextColor(RGB(0, 0, 0));
			pDC->SetBkColor(RGB(255,255,255));
			break;
		case CTLCOLOR_LISTBOX:
			pDC->SetTextColor(RGB(0, 0, 0));
			pDC->SetBkColor(RGB(255,255,255));
			break;
		case CTLCOLOR_SCROLLBAR:
			pDC->SetTextColor(RGB(0, 0, 0));
			pDC->SetBkColor(RGB(255,255,255));
			break;
		case CTLCOLOR_BTN:
			pDC->SetTextColor(RGB(0, 0, 0));
			pDC->SetBkColor(RGB(255,255,255));
			break;
		case CTLCOLOR_DLG:	    
			return hbr;
	}	
	
	// TODO: Return a different brush if the default is not desired
	return hbr;
}

void CWizardPage::OnPaint(void)
{
	CDialog::OnPaint();
}



BOOL  CWizardPage::OnEraseBkgnd(CDC* pDC)
{
	return TRUE;
}

// refer to CPropertyPage class members for a description of this function
void CWizardPage::OnCancel()
{
}


// refer to CPropertyPage class members for a description of this function
BOOL CWizardPage::OnKillActive()
{
	return TRUE;
}



// called when the page is set active
void CWizardPage::OnSetActive()
{
	LocalizeControls();

	m_pParent->background = background;
	m_pParent->backgroundLocalized = backgroundLocalized;
	

	m_pParent->Invalidate();
	Invalidate();
}



// refer to CPropertyPage class members for a description of this function
BOOL CWizardPage::OnQueryCancel()
{
	return TRUE;
}


// refer to CPropertyPage class members for a description of this function
// return -1 to prevent changing pages
// to move to a page other than the previous one, return the 
// Dialog resource ID of the page to display
LRESULT CWizardPage::OnWizardBack()
{
	return 0;
}


// refer to CPropertyPage class members for a description of this function
// return -1 to prevent changing pages
// to move to a page other than the next one, return the 
// Dialog resource ID of the page to display
LRESULT CWizardPage::OnWizardNext()
{
	return 0;
}



// refer to CPropertyPage class members for a description of this function
BOOL CWizardPage::OnWizardFinish()
{
	return TRUE;
}


// ExecutionFeedback
void CWizardPage::ProcessFeedback( void )
{
}


// Progress
void CWizardPage::ReportProgress(CString text, long current, long end)
{
}

// Localisation
void CWizardPage::LocalizeControls()
{
	background = CSetupApp::SetupApp->GetBitmapResource(backgroundFile);	
	backgroundLocalized = CSetupApp::SetupApp->GetLocalizedBitmapResource(backgroundFile);
	if( m_pParent != 0 )
	{
		m_pParent->background = background;
		m_pParent->backgroundLocalized = backgroundLocalized;
	}
}


unsigned long long CWizardPage::DownloadFile(CString url, CString destPath, CString progressMessage )
{
	CInternetSession session;
	char buffer[1024];
	unsigned long long retValue = 0;

	CStdioFile * file = session.OpenURL(url,1,INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_RELOAD);
	CFile target;
	target.Open(destPath,CFile::typeBinary | CFile::modeCreate | CFile::modeWrite );

	long filesize = (long) file->SeekToEnd();
	file->SeekToBegin();
	long current = 0;

	CString message;
	message.Format(progressMessage,current / 1024,filesize / 1024);;
	ReportProgress(message,0,0);
	PeekAndPump();
	retValue = filesize << 8;
	while( filesize > current )
	{
		long bytes = file->Read(buffer,min( filesize-current,sizeof(buffer)));
		target.Write(buffer,bytes);
		for( int index = 0; index < bytes; ++ index )
		{
			retValue = retValue ^ ((unsigned char ) buffer[index]);
			retValue = _rotl64(retValue,53);

		}

		message.Format(progressMessage,current / 1024,filesize / 1024);
		ReportProgress(message,current,filesize);
		PeekAndPump();
		

		current += sizeof(buffer);
	}
	file->Close();
	message.Format(progressMessage,filesize / 1024,filesize / 1024);
	ReportProgress(message,filesize,filesize);
	return retValue;
}

